/***
 * 公共的方法
 ****/
import {
	getToken
} from '@/utils/token.js'
export function uploadFile(file,code,fileSize = 2,fileType=100){
	return new Promise((resolve, reject) => {
	uni.showLoading({
		title: '上传中'
	})
	if(file.size > (1024*1024 * Math.abs(fileSize))) {
		uni.showToast({
			title:`大小请勿超过${fileSize}M`,
			icon:'none'
		})
		uni.hideLoading()
		return;
	}
	uni.uploadFile({
		url: 'https://job.ydylmold.cn/base/upload/file', //仅为示例，非真实的接口地址
		filePath: file.url,
		name: 'file',
		formData: {
			'fileType': fileType,
			'fileCode': code,
			'fileParaName': 'file'
		},
		header:{
			'token': getToken('token')
		},
		success: (uploadFileRes) => {
			uni.hideLoading()
			let dataImg = JSON.parse(uploadFileRes.data)
			if (dataImg.code == 200) {
				uni.hideLoading()
				resolve(dataImg.data)
			} else {
				uni.showToast({
					title:'上传出错',
					icon:'none'
				})
			}
		},
		fail:(error)=>{
			uni.hideLoading()
			uni.showToast({
				title:'上传出错',
				icon:'none'
			})
		}
		})
	})
}

export function uploadVideo(code) {
	return new Promise((resolve, reject) => {
		uni.chooseVideo({
			 count: 1,
			sourceType: ['camera', 'album'],
			sizeType: ['compressed'],
			name: 'file',
			success: res => {
				//console.log(res.tempFilePath,8888);
				uni.showLoading({
					title: '上传中'
				})
				uni.uploadFile({
					url: 'https://job.ydylmold.cn/base/upload/file', //仅为示例，非真实的接口地址
					filePath: res.tempFilePath,
					name: 'file',
					formData: {
						'fileType': 101,
						'fileCode': code,
						'fileParaName': 'file'
					},
					success: (uploadFileRes) => {
						let dataImg = JSON.parse(uploadFileRes.data)
						if (dataImg.code == 200) {
							uni.hideLoading()
							resolve(dataImg.data)
						} else {
							reject(res.message)
						}
					},
					fail:(err)=>{
						//console.log(err)
					}
				});
			}
		});
	})
}


